package ${localPackage}.service.impl;


import ${localPackage}.utils.DateTools;
import ${localPackage}.utils.ReJson;
import ${localPackage}.utils.sql.SqlUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
* Created by chenyuncong on 2018/x/x.
*
*/
@Service("${gvo.headSmallEntityName}Service")
public class ${gvo.entityName}ServiceImpl implements ${gvo.entityName}Service {

    @Autowired
    private ${gvo.entityName}Dao ${gvo.headSmallEntityName}Dao;

    /**
    * 添加
    */
    @Override
    public Integer save(${gvo.entityName}VO vo){
        ${gvo.entityName} o =new ${gvo.entityName}(${gvo.voParams});
        return ${gvo.headSmallEntityName}Dao.save(o);
    }

    //查询结果
    @Override
    public ReJson get${gvo.entityName}List(${gvo.entityName}VO vo){
        ReJson json = new ReJson();
//      条件字段
        StringBuffer paramSql = new StringBuffer(" from ${gvo.spaceEntityName} where 1=1 ");
        List<Object> pList = new ArrayList<>();
        //需要条件自己加
<#list gvo.propertyVoList as p>
    <#if p.propertyName == "id">
    <#else>
        <#if p.isSearch==1 >
        if(!StringUtils.isEmpty(vo.get${p.headBigPropertyName}())) {
        <#if p.propertyType="String">
            paramSql.append(" and ${p.spacePropertyName} like ? ");
            pList.add("%" + vo.get${p.headBigPropertyName}() + "%");
        <#else>
            paramSql.append(" and ${p.spacePropertyName} = ? ");
            pList.add(vo.get${p.headBigPropertyName}());
        </#if>
        }
        </#if>
    </#if>
</#list>


    //  获取对应条件的总数
        String countSql = " select count(1) " + paramSql.toString();
        int count = ${gvo.headSmallEntityName}Dao.count(countSql, pList);
    //  获取对应数据
        String sql = "select * " + paramSql.toString()+" order by id desc " + " limit " + vo.getStartNum() + "," + vo.getLimit();
        List<${gvo.entityName}> list = ${gvo.headSmallEntityName}Dao.finds(sql, ${gvo.entityName}.class, pList);

        json.set(0, count, list, "成功");
        return json;
    }

    //根据id获取对象
    @Override
    public ${gvo.entityName} get${gvo.entityName}ById(Integer id){
        return ${gvo.headSmallEntityName}Dao.get(id);
    }

    //更新对象
    @Override
    public void update(${gvo.entityName}VO vo){
        ${gvo.entityName} o = ${gvo.headSmallEntityName}Dao.get(vo.getId());
        o.set${gvo.entityName}(${gvo.voParams});
        ${gvo.headSmallEntityName}Dao.update(o);
    }

    //删除数据
    @Override
    public void delete(Integer [] ids){
        ${gvo.headSmallEntityName}Dao.batchDelete(ids);
    }

}